﻿using Mobileterminal.Common;
using Mobileterminal.Dtos;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms
{
    public partial class AddQueryForm : UIEditForm
    {
        public static List<AppsTypes> appsTypes = new List<AppsTypes>();

     
        public AddQueryForm()
        {
            InitializeComponent();
            GetApps();


        }


        //获取分类对应的分类名称--下拉框
        private async Task GetApps()
        {
            var url = "https://localhost:7224/api/ApparatusType/QueryApparatusType";


            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<AppsTypes>>>(json);
            if (result.Code == 200)
            {
                uiComboBox1.DataSource = result.Data;
                uiComboBox1.DisplayMember = "ApparatusTypeName";
                uiComboBox1.ValueMember = "ApparatusTypeId";
                appsTypes = result.Data;
            }
        }


       

        private void AddQueryForm_Load(object sender, EventArgs e)
        {

        }


        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }


        private void rbMale_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbMale.Checked)
            {
                this.rbMale1.Checked = false;
            }
        }
        private void uiRadioButton1_CheckedChanged(object sender, EventArgs e)
        {

            if (this.rbMale1.Checked)
            {
                this.rbMale.Checked = false;
            }
        }
        private void uiLabel7_Click(object sender, EventArgs e)
        {

        }



        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 获取所选的CustomerID
            if (uiComboBox1.SelectedValue != null)
            {
                int customerId = Convert.ToInt32(uiComboBox1.SelectedValue);
                Console.WriteLine($"Selected Customer ID: {customerId}");
            }

        }


        //确定按钮
        private async void btnOK_Click(object sender, EventArgs e)
        {
            var type = appsTypes.FirstOrDefault(x => x.ApparatusTypeName == uiComboBox1.Text);
            if (type == null)
            {
                UIMessageTip.Show("请选择分类");
                return;
            }

            if (string.IsNullOrEmpty(uiTextBox1.Text))
            {
                UIMessageTip.Show("请输入器械编号");
                return;
            }
            if (string.IsNullOrEmpty(uiTextBox2.Text))
            {
                UIMessageTip.Show("请输入器械名称");
                return;
            }

            //传参
            var pdata = new
            {
                apparatusId = 0,
                ApparatusTypeId = type.ApparatusTypeId,
                ApparatusNo = uiTextBox1.Text,
                ApparatusName = uiTextBox2.Text,
                ApparatusModel = uiTextBox3.Text,
                ApparatusNnit = uiTextBox4.Text,
                ApparatusPrice = uiTextBox5.Text,
                Durableproperties = this.rbMale.Checked ? 1 : 2,
            };

            string jsonParam = JsonConvert.SerializeObject(pdata);
            HttpContent httpContent = new StringContent(jsonParam, Encoding.UTF8, "application/json");
            var json = await HttpClientHelper.ClientAsync("post", "https://localhost:7286/api/Apparatus/CreateApparatus", true, httpContent);
            ApparaData result = JsonConvert.DeserializeObject<ApparaData>(json);

              if (result != null)
            {
                UIMessageTip.Show("添加成功");

                this.DialogResult = DialogResult.OK;  // 先设置结果
                this.Close();// 关闭窗口
                return;
            }



        }


        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {

        }
    }
}
